package com.oocl.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.oocl.dao.FoodDao;
import com.oocl.factory.DBConFactory;
import com.oocl.log4j.Log4j;
import com.oocl.pojo.Food;
import com.oocl.pojo.FoodType;
import com.oocl.util.DBUtil_JNDI;

public class FoodDaoImpl implements FoodDao {

	@Override
	public int save(Food food) {
		String sql = "insert into food(id, name, price, img, type_id， merchant_id) values(?, ?, ?, ?, ?， ?)";
		Connection con = null;
		PreparedStatement pst = null;
		int m = 0;
		try {
			con = DBConFactory.getConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, food.getId());
			pst.setString(2, food.getName());
			pst.setDouble(3, food.getPrice());
			pst.setString(4, food.getImg());
			;
			pst.setString(5, food.getTypeId());
			pst.setString(6, food.getMerchantId());
			m = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBConFactory.close(con, pst, null);
		}
		return m;
	}

	@Override
	public int delete(String id) {
		String sql = "delete from food where id = ?";
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		int m = 0;
		try {
			con = DBConFactory.getConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, id);
			m = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBConFactory.close(con, pst, rs);
		}
		return m;
	}

	@Override
	public int update(Food menu) {
		String sql = "update food set name = ?, price = ?, img = ?, type_id =? , merchant_id = ? where id = ?";
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		int m = 0;
		try {
			con = DBConFactory.getConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, menu.getName());
			pst.setDouble(2, menu.getPrice());
			pst.setString(3, menu.getImg());
			pst.setString(4, menu.getTypeId());
			pst.setString(5, menu.getMerchantId());
			pst.setString(6, menu.getId());
			m = pst.executeUpdate();
		} catch (SQLException e) {
			Log4j.getLogger().error("[ update food 信息出错 ] {}", e.fillInStackTrace());
		} finally {
			DBConFactory.close(con, pst, rs);
		}
		return m;
	}

	@Override
	public Food find(String id) {
		String sql = "select * from food where id = ?";

		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			con = DBConFactory.getConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, id);
			rs = pst.executeQuery();
			while (rs.next()) {
				String r_id = rs.getString("id");
				String name = rs.getString("name");
				double price = rs.getDouble("price");
				String img = rs.getString("img");
				String typeId = rs.getString("type_id");
				String merchantId = rs.getString("merchant_id");
				Food food = new Food(r_id, name, price, img, typeId, merchantId);
				return food;
			}
		} catch (SQLException e) {
			Log4j.getLogger().error("[ 由 id 查找 Food 信息出错 ] {}", e.fillInStackTrace());
		} finally {
			DBConFactory.close(con, pst, rs);
		}
		return null;
	}

	@Override
	public List<Food> findByMerchant(String merchantId) {
		String sql = "select * from food where merchant_id = ?";
		List<Food> munuDOList = new ArrayList<Food>();
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			con = DBConFactory.getConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, merchantId);
			rs = pst.executeQuery();
			while (rs.next()) {
				String r_id = rs.getString("id");
				String name = rs.getString("name");
				double price = rs.getDouble("price");
				String img = rs.getString("img");
				String typeId = rs.getString("type_id");
				String mId = rs.getString("merchant_id");
				Food menu = new Food(r_id, name, price, img, typeId, mId);
				munuDOList.add(menu);
			}
		} catch (SQLException e) {
			Log4j.getLogger().error("[ 由 merchantId 查找 Merchant出错 ] {}", e.fillInStackTrace());
		} finally {
			DBConFactory.close(con, pst, rs);
		}
		return munuDOList;
	}

	@Override
	public List<Food> findByType(String typeId) {
		String sql = "select * from food where type_id = ?";
		List<Food> munuDOList = new ArrayList<Food>();
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			con = DBConFactory.getConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, typeId);
			rs = pst.executeQuery();
			while (rs.next()) {
				String r_id = rs.getString("id");
				String name = rs.getString("name");
				double price = rs.getDouble("price");
				String img = rs.getString("img");
				String tId = rs.getString("type_id");
				String merchantId = rs.getString("merchant_id");
				Food menu = new Food(r_id, name, price, img, tId, merchantId);
				munuDOList.add(menu);
			}
		} catch (SQLException e) {
			Log4j.getLogger().error("[ 由 typeId 查找 List<Food>出错 ] {}", e.fillInStackTrace());
		} finally {
			DBConFactory.close(con, pst, rs);
		}
		return munuDOList;
	}

	@Override
	public List<Food> findAll() {
		String sql = "select * from food";
		List<Food> munuDOList = new ArrayList<Food>();
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			con = DBConFactory.getConnection();
			pst = con.prepareStatement(sql);
			rs = pst.executeQuery();
			while (rs.next()) {
				String r_id = rs.getString("id");
				String name = rs.getString("name");
				double price = rs.getDouble("price");
				String img = rs.getString("img");
				String tId = rs.getString("type_id");
				String merchantId = rs.getString("merchant_id");
				Food menu = new Food(r_id, name, price, img, tId, merchantId);
				munuDOList.add(menu);
			}
		} catch (SQLException e) {
			Log4j.getLogger().error("[ 查找所有的食品信息出错 ] {}", e.fillInStackTrace());
		} finally {
			DBConFactory.close(con, pst, rs);
		}
		return munuDOList;
	}

	@Override
	public List<FoodType> findFoodTypes() {
		String sql = "select * from foodtype";
		List<FoodType> foodTypes = new ArrayList<FoodType>();
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			con = DBConFactory.getConnection();
			pst = con.prepareStatement(sql);
			rs = pst.executeQuery();
			while (rs.next()) {
				String id = rs.getString("id");
				String typeName = rs.getString("type_name");
				
				FoodType foodType = new FoodType(id, typeName);
				foodTypes.add(foodType);
			}
		} catch (SQLException e) {
			Log4j.getLogger().error("[ 查找所有的食品类型信息出错 ] {}", e.fillInStackTrace());
		} finally {
			DBConFactory.close(null, pst, rs);
		}
		return foodTypes;
	}

	@Override
	public List<Food> findByTypeIdAndMerchantId(String typeId, String merchantId) {
		String sql = "select * from food where type_id = ? and merchant_id = ? ";
		List<Food> foods = new ArrayList<Food>();
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			con = DBConFactory.getConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, typeId);
			pst.setString(2, merchantId);
			rs = pst.executeQuery();
			while (rs.next()) {
				String r_id = rs.getString("id");
				String name = rs.getString("name");
				double price = rs.getDouble("price");
				String img = rs.getString("img");
				String tId = rs.getString("type_id");
				String mId = rs.getString("merchant_id");
				Food food = new Food(r_id, name, price, img, tId, mId);
				foods.add(food);
			}
		} catch (SQLException e) {
			Log4j.getLogger().error("[ 由 typeId，  merchantId 查找 List<Food>出错 ] {}", e.fillInStackTrace());
		} finally {
			DBConFactory.close(con, pst, rs);
		}
		return foods;
	}

	@Override
	public List<Food> findByFuzzy(String keyWord) {
		List<Food> foods = new ArrayList<Food>();
		try {
			String sql = "select * from food where name like '%"+keyWord+"%'";

			Connection con = null;
			Statement st = null;
			ResultSet rs = null;
			con = DBConFactory.getConnection();
			st = con.createStatement();
			rs = st.executeQuery(sql);
				while (rs.next()) {
					String r_id = rs.getString("id");
					String name = rs.getString("name");
					double price = rs.getDouble("price");
					String img = rs.getString("img");
					String tId = rs.getString("type_id");
					String mId = rs.getString("merchant_id");
					Food food = new Food(r_id, name, price, img, tId, mId);
					foods.add(food);
			}
			DBConFactory.close(con, st, rs);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return foods;
	}

}
